package cn.edu.hnu.gpg.controller;

import cn.edu.hnu.gpg.entity.Question;
import cn.edu.hnu.gpg.entity.Questionnaire;
import cn.edu.hnu.gpg.entity.Student;
import cn.edu.hnu.gpg.entity.User;
import cn.edu.hnu.gpg.service.QuestionService;
import cn.edu.hnu.gpg.service.QuestionnaireService;
import cn.edu.hnu.gpg.dao.PlanMapper;
import cn.edu.hnu.gpg.dto.PhaseWithDocInfo;
import cn.edu.hnu.gpg.dto.StudentDocumentInfo;
import cn.edu.hnu.gpg.entity.Plan;
import cn.edu.hnu.gpg.entity.PlanExample;
import cn.edu.hnu.gpg.entity.User;
import cn.edu.hnu.gpg.service.StudentDocService;
import cn.edu.hnu.gpg.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    private final StudentService studentService;
    private final QuestionService questionService;
    private final QuestionnaireService questionnaireService;
    private final StudentDocService docService;
    private final PlanMapper planMapper;

    @Autowired
    public StudentController(StudentService studentService,QuestionService questionService,QuestionnaireService questionnaireService, StudentDocService docService, PlanMapper planMapper){
        this.studentService = studentService;
        this.questionService = questionService;
        this.questionnaireService = questionnaireService;
        this.docService = docService;
        this.planMapper = planMapper;
    }

    @RequestMapping("/add_question")
    public String questionManagement() {
        return "jsp/student/add_question.jsp";
    }

    @RequestMapping("/change_password")
    public String ChangePassword() {
        return "jsp/student/change_studentpw.jsp";
    }

    @RequestMapping("/update_info")
    public String UpdateInfo(){return "jsp/student/update_info.jsp";}

    @RequestMapping("/select_teacher")
    public String SelectTeacher(){return "jsp/student/select_teacher.jsp";}

    @RequestMapping("/search_question")
    public String SearchQuestion(HttpServletRequest request,Map<String,Object> model){
        User user =(User)request.getSession(true).getAttribute("user");
        List<Question> questions = questionService.findQuestionByUserId(user.getUserId());
        if (questions == null)
            return "jsp/student/search_question404.jsp";
        model.put("questions", questions);
        return "jsp/student/search_question.jsp";
    }

    @RequestMapping("/get_questionnaire")
    public String getQuestionnaire(HttpServletRequest request,Map<String,Object> model){
        List<Questionnaire> questionnaires = questionnaireService.getAllQuestionnaire();
        //System.out.println(questionnaires.get(0).getOptions());
        model.put("questionnaires", questionnaires);
        return "jsp/student/questionnaire.jsp";
    }

    @RequestMapping("/tasks_center")
    public String documentManagement(HttpServletRequest request, Map<String,Object> model) {

        List<StudentDocumentInfo> documentInfoList = docService.
                listDocumentByUserId(((User) request.getSession().getAttribute("user")).getUserId());
        List<Plan> planList = planMapper.selectByExample(new PlanExample());

        List<PhaseWithDocInfo> phaseList = new ArrayList<>(planList.size());
        for (Plan p : planList) {
            PhaseWithDocInfo phase = new PhaseWithDocInfo();
            phase.setPlan(p);
            phaseList.add(phase);
        }

        Iterator<PhaseWithDocInfo> iterator = phaseList.iterator();
        PhaseWithDocInfo phase = iterator.next();

        if (documentInfoList != null) {
            for (StudentDocumentInfo docInfo : documentInfoList) {
                while (phase.getPlan().getPhaseId() < docInfo.getPhaseId() && iterator.hasNext()) {
                    phase = iterator.next();
                }
                phase.setDocumentInfo(docInfo);
            }
        }

        model.put("phaseList", phaseList);
        return "jsp/student/tasks_center.jsp";
    }
}
